<?php

include_once("interface.php");

class Installation extends Data
{
    private $fn = "/etc/lab/lab.conf";
    private $cmd = "/etc/lab/install_to_hd.sh";
    private $mntpnt = "/mnt/root";
    public $title = "Installation";
    public $module = "installation";
    public $data;

    public function __construct() {
        $this->data = array (   "type" => "live",
                                "disk" => array ()
                            );
    }

    //read the data from files and return a complete object
    function readDataFromSystem() {
        $ret = 0;
        $filearray = file($this->fn);
        if ( $filearray == false ) {
            return false;
        }
        foreach ( $filearray as $arrval ) {
            if(preg_match('/INSTALL_TYPE.*/', $arrval)) {
                $tmp = preg_split('/=/', trim($arrval));
                break;
            }
        }
        $this->data['type'] = $tmp[1];

        if($this->data['type'] == "LIVE") {
            $dn = "/sys/class/block";
            $filearray = scandir($dn);

            if ( $filearray == false ) {
                return false;
            }
            foreach ( $filearray as $arrval ) {
                if ( preg_match("/[s,h]d[a-z]$/", $arrval) ) {
                    array_push($this->data["disk"], trim($arrval));
                }
            }
        }
        else {
            $tmp1 = preg_split('/ /', $tmp[1]);
            array_push($this->data["disk"], $tmp1[1]);
        }

        error_log("Installation readDataFromSystem(): read from $this->fn");
        return $this->data;
    }

    //write the obj to the file
    function commitDataToSystem($obj) {
        $ret;
        $retstr = "Successfully installed to ".$obj['disk'][0];
        $out = array();
        $cmd = $this->cmd." /dev/".$obj['disk'][0];
        error_log("Installation commitDataToSystem(): executing ".$cmd);
        passthru($cmd, &$ret); 
        if($ret != 0) {
            $retstr = "Failed to install to ".$obj['disk'][0];
        }
        else {
            $cmd = "sed -i -r 's/^INSTALL_TYPE.*/INSTALL_TYPE=".$obj['type']." ".$obj['disk'][0]."/' ".$this->fn;
            exec($cmd, &$out, &$ret);
            if($ret != 0) {
                $retstr = "Fail to change system type";
            }
            else {
                $ret = copy($this->fn, $this->mntpnt."1".$this->fn);
                $ret = copy($this->fn, $this->mntpnt."2".$this->fn);
                if($ret == false) {
                    $retstr = "Fail to copy to install location $this->mntpnt";
                }
            }
        }
        return $retstr;
    }

    function printModuleHTML() {
        $str = <<<END

    <form id="instform">
    <fieldset id="disk" class="ui-widget-content">
    </fieldset>
    <br>
    </form>
    <button id="submit" class="ui-widget-button">Install</button>
    <br>
    <div id="info" class=""></div>

END;

        return $str;
    }

    function printModuleScript() {
        $str = <<<END

<script type="text/javascript">

    //fill in the form using values received from server
    function writeDataToHTML(data) {
        var html = "<legend>Installation</legend><br>";
        if(data.type === "LIVE") {
            html += "The appliance is running as a Live system, To install in on the hard disk ";
            html += "please select the appropriate disk to install on:<br><br>";
            for ( i=0; i < data.disk.length; i++ ) {
                html += "<input type='radio' name='disk' value='"+ data.disk[i] +"' />/dev/" + data.disk[i] +"<br>";
            }
            html += "<br><br>The installation will wipe out the entire disk and create the following partitions:<br>";
            $("#submit").show();
        }
        else {
            html += "The appliance software is already installed on the disk <b>/dev/" + data.disk[0] + "</b><br><br>";
            html += "It has the following partitions:<br>";
            $("#submit").hide();
        }

        html += "<pre>";
        html += "<b>Name    Partition    Mount     Size<br></b>";
        html += "boot    partition1   /boot     100M<br>";
        html += "root    partition2   /         600M<br>";
        html += "root2   partition3   /backup   600M<br>";
        html += "swap    partition5             10% of the disk <br>";
        html += "var     partition6   /var      20% of the disk <br>";
        html += "data    partition7   /data     Rest of the disk<br>";
        html += "</pre>";

        $("#disk").html(html);
    }

    //fill in the obj to be sent to the server using the form values
    function readDataFromHTML() {
        var data = Object();
        data.disk = Array();
        data.disk[0] = $("#instform input[type='radio']:checked").val();
        data.type = "ONDISK";
        return data;
    }

    function validate(obj) {
        var msg = null;
        if(obj.disk[0] === undefined) {
            msg = "Please select a disk to install the image";
        }
        return msg;
    }

    //called before sending the data to the server
    function beforeSendFunction() {
        $("#info").html("Installing to hard-disk ... ");
        $("#info").dialog({
            width: 600,
            position: 'top',
            modal: true
        });

    }

    //called when the submit operation succeeds
    function successFunction(msg) {
        var html = "The appliance software is installed on the disk  <br><br>";
        $("#submit").hide();
        $("#info").append("Done <br><br><pre>" + msg + "<br></pre>");
        $("#disk").html(msg);
    }

    //called when the submit operation fails
    function errorFunction(msg) {
        $("#info").append("Error: " + msg);
    }

</script>

END;
        return $str;
    }

}

?>
